package app.android.com.wuandroidapp;

import android.app.Application;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.v4.content.ContextCompat;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.utils.DistanceUtil;
import com.squareup.leakcanary.LeakCanary;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import app.android.com.utilscode.util.CrashUtils;
import app.android.com.utilscode.util.LogUtils;
import app.android.com.utilscode.util.Utils;
import app.android.com.wuandroidapp.test.model.db.dao.PasswordUtils;


/**
 * Created by admin on 2017/4/6.
 */

public class AndroidApplication extends Application {
    private static AndroidApplication applicationInstance;
    ApplicationComponent applicationComponent;
    public static PasswordUtils passwordUtils;

    public LocationClient mLocationClient;// 定位SDK的核心类
    public BDLocation locations;

    public static AndroidApplication getInstance() {

        return applicationInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        SDKInitializer.initialize(this);
        applicationComponent = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this))
                .build();
        applicationInstance = this;
        createFile();
        utildcode();
        passwordUtils = new PasswordUtils(this);
        initLocation();
    }

    public void createFile() {
        //电话监听文件类
        File phonefile = new File(AppConstants.WUAPPPHONE);
        if (phonefile.exists()) {
            phonefile.mkdirs();
        }
    }

    public ApplicationComponent getApplicationComponent() {

        return applicationComponent;
    }

    /**
     * 初始化工具类
     */
    public static LogUtils.Builder lBuilder;

    public void utildcode() {
        // 内存泄露检查工具
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);
        //初始化工具类
        Utils.init(this);
        CrashUtils.getInstance().init();
        lBuilder = new LogUtils.Builder()
                .setLogSwitch(BuildConfig.DEBUG)// 设置log总开关，默认开
                .setGlobalTag("CMJ")// 设置log全局标签，默认为空
                // 当全局标签不为空时，我们输出的log全部为该tag，
                // 为空时，如果传入的tag为空那就显示类名，否则显示tag
                .setLog2FileSwitch(false)// 打印log时是否存到文件的开关，默认关
                .setBorderSwitch(true)// 输出日志是否带边框开关，默认开
                .setLogFilter(LogUtils.V);// log过滤器，和logcat过滤器同理，默认Verbose

    }


    /**
     * 百度定位
     */
    public void initLocation() {
        mLocationClient = new LocationClient(getApplicationContext());
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式
        option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
        // option.setScanSpan(300000);// 设置发起定位请求的间隔时间为5分钟
        option.setScanSpan(5 * 60 * 1000);
        option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
        option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
        //   option.setScanSpan(60000);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setLocationNotify(false);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIgnoreKillProcess(true);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        mLocationClient.setLocOption(option);
        MyLocationListener mMyLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(mMyLocationListener);
        startLocation();
    }


    public void startLocation() {
        mLocationClient.start();
    }

    public void stopLocation() {
        mLocationClient.stop();
    }

    /**
     * 定位回调
     */

    boolean islocation = false;
    public double lat, lon;

    public class MyLocationListener implements BDLocationListener {

        public void onReceiveLocation(BDLocation location) {
            locations = location;

            lat = location.getLatitude();
            lon = location.getLongitude();

        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }
    }


}
